package cn.qyl.joha.controller.admin;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.qyl.joha.common.PageConstant;
import cn.qyl.joha.common.PageModel;
import cn.qyl.joha.dto.Goods;
import cn.qyl.joha.dto.Timlimited;
import cn.qyl.joha.service.TimeService;
import cn.qyl.joha.service.Impl.TimeServiceImpl;




@Controller
@RequestMapping("/admin/time")
public class TimeController {
	
	TimeService timeService = new TimeServiceImpl();
	//获取限时抢购列表
	@RequestMapping("getTimeList")
	public String TimeMapper(Timlimited time ,ModelMap map,String pageIndex){
		//创建分页模型
		PageModel pageModel = new PageModel();
		//当前页
		if(pageIndex!=null && pageIndex!=""){
			pageModel.setPageIndex(Integer.parseInt(pageIndex));
		}else{
			pageModel.setPageIndex(0);
		}
		pageModel.setPageSize(3);
		//总记录数
		int recordCount = timeService.getCountByTime(time);
		pageModel.setRecordCount(recordCount);
		List<Timlimited> times = timeService.getTime(time,pageModel);
		map.put("times", times);
		map.addAttribute("time",time);
		map.addAttribute("pageModel",pageModel);
		return "time/timeLimitList";	
		}
		
		//删除限时抢购商品
		@RequestMapping("delTimeById")
		public String delTimeById(int id, ModelMap map){
			
			int count = timeService.delTimeById(id);
			if(count>0){
				map.put(PageConstant.TIP, "删除成功");
			}else{
				map.put(PageConstant.TIP,"删除失败");
			}
	
			return "forward:getTimeList";
		}
		//批量删除限时抢购商品
		@RequestMapping("delTimeByTids")
		public String delTimeByTids(int[] tids,ModelMap map){
			
			int count = timeService.delTimeByTids(tids);
			
			if(count>0){
				map.put(PageConstant.TIP, "删除成功");
			}else{
				map.put(PageConstant.TIP, "删除失败");
			}
			return "forward:getTimeList";
		}
		
		//跳转新增限时抢购页面
		@RequestMapping("toAddTime")
		public String toAddTime(){
			return "time/addTime";
		}
		
		//新增新增限时抢购
		@RequestMapping("addTime")
		public String addTime(Timlimited time,ModelMap map){
			
			timeService.addTime(time);
			
			map.put(PageConstant.TIP, "新增成功");
			
			return "time/addTime";
		}
		
		//跳转更新限时抢购页面
		@RequestMapping("updateTimeById")
		public String updateTimeById(int id,ModelMap map){
			
			Timlimited time = timeService.updateTimeById(id);
			
			map.addAttribute("time",time);
			
			return "time/updateTime";
		}
		
		@RequestMapping("updateTime")
		public String updateTime(Timlimited time,ModelMap map){
			
			int count = timeService.updateTime(time);
			
			if(count >0){
				map.put(PageConstant.TIP, "修改成功");
			}else{
				map.put(PageConstant.TIP, "修改失败");
			}
			
			return "forward:updateTimeById";
			
		}


}